/********************************************************************************
Copyright (C), TianNeng Power. Ltd.
Author: 	hxbao
Version: 	V0.0
Date: 		2022/5/3
History:
	V0.0		2022/5/3		 Preliminary
********************************************************************************/
#include "includes.h"

OneBusBitSendCfg_t OneBusSendCfg;
OneBusBitSendSta_t OneBusSta;

/**
 * @brief  Configures tim1 clocks.
 */
void TIM3_Configuration(void)
{
    TIM_TimeBaseInitType TIM_TimeBaseStructure;
    uint16_t PrescalerValue = 0;
    /* Compute the prescaler value */
    //ftcnt = 1Mhz
    PrescalerValue = 47;//(uint16_t) (SystemCoreClock / 12000000) - 1;

    /* Time base configuration */
    TIM_TimeBaseStructure.Period    = 65535;
    TIM_TimeBaseStructure.Prescaler = 0;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;

    TIM_InitTimeBase(TIM3, &TIM_TimeBaseStructure);

    /* Prescaler configuration */
    TIM_ConfigPrescaler(TIM3, PrescalerValue, TIM_PSC_RELOAD_MODE_IMMEDIATE);

    /* TIM1 enable update irq */
    TIM_ConfigInt(TIM3, TIM_INT_UPDATE, ENABLE);
}



void Niu_OneBusTimerInit()
{
    NVIC_InitType NVIC_InitStructure;
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);

    TIM3_Configuration();
        

    /* Enable the TIM1 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;

    NVIC_Init(&NVIC_InitStructure);
}

void Niu_OneBusTimerStart()
{
    //1us 马上开始
    
    TIM_SetCnt(TIM3, 0);
    TIM_SetAutoReload(TIM3,10);
    TIM_Enable(TIM3, ENABLE);
}

void Niu_OneBusTimerStop()
{
    //TMR0_Disable();
    TIM_Enable(TIM3, DISABLE);
}

void Niu_OneBusSetDelay(uint16_t dly)
{
    //TMR0_TimerInit(dly*FREQ_SYS / 1000000);//1us
    TIM_SetCnt(TIM3, 0);
    // TIM_Enable(TIM3, DISABLE);
    TIM_SetAutoReload(TIM3,dly);
    //TIM_Enable(TIM3, ENABLE);
}

uint8_t byteIndex;
uint8_t bitIndex;
uint8_t curData;

void TIM3_IRQHandler(void) // TMR3 定时中断
{

    if (TIM_GetIntStatus(TIM3, TIM_INT_UPDATE) != RESET)
    {
        //TMR0_ClearITFlag(TMR0_3_IT_CYC_END); // 清除中断标志
        TIM_ClrIntPendingBit(TIM3, TIM_INT_UPDATE);

        switch(OneBusSta.bitPhase)
        {
            //1-同步头低电平阶段    2-同步头高电平阶段       3 数据低电平阶段   4 数据高电平  5 -Stop低阶段   6-stop高电平
            case 1://
                //GPIOA_ResetBits(TN_ONE_TX_PAPIN);
                MCU_GPIO_ClrBit(TN_ONE_TX_PORT, TN_ONE_TX_PIN);
                //设置延迟
                Niu_OneBusSetDelay(OneBusSendCfg.syncLDelayTime);

                OneBusSta.bitPhase = 2;
                break;
            case 2:
                //GPIOA_SetBits(TN_ONE_TX_PAPIN);
                MCU_GPIO_SetBit(TN_ONE_TX_PORT, TN_ONE_TX_PIN);
                //设置延迟
                Niu_OneBusSetDelay(OneBusSendCfg.syncHDelayTime);
                OneBusSta.bitPhase = 3;
                break;
            case 3:
                byteIndex = OneBusSta.bitIndex/8;
                bitIndex = 7-OneBusSta.bitIndex%8;
                curData = *(OneBusSta.buf+byteIndex);
                //GPIOA_ResetBits(TN_ONE_TX_PAPIN);
                MCU_GPIO_ClrBit(TN_ONE_TX_PORT, TN_ONE_TX_PIN);

                if(curData & ((uint8_t)0x01 << bitIndex))//bit == 1
                {
                    //设置延迟
                    Niu_OneBusSetDelay(OneBusSendCfg.Data1LDelayTime);
                }else {
                    //设置延迟
                    Niu_OneBusSetDelay(OneBusSendCfg.Data0LDelayTime);
                }
                OneBusSta.bitPhase = 4;

                break;
            case 4:

                // if((OneBusSta.bitIndex++) == OneBusSta.bitSize)
                // {
                //     OneBusSta.bitPhase = 5;                    
                // }else {
                //     //GPIOA_SetBits(TN_ONE_TX_PAPIN);
                //     MCU_GPIO_SetBit(TN_ONE_TX_PORT, TN_ONE_TX_PIN);

                //      if(curData & ((uint8_t)0x01 << bitIndex))
                //     {
                //         Niu_OneBusSetDelay(OneBusSendCfg.Data1HDelayTime); //设置延迟
                //         //设置延迟
                //     }else {
                //         Niu_OneBusSetDelay(OneBusSendCfg.Data0HDelayTime); //设置延迟
                //     }
                //     OneBusSta.bitPhase = 3;
                // }


                MCU_GPIO_SetBit(TN_ONE_TX_PORT, TN_ONE_TX_PIN);
                if(curData & ((uint8_t)0x01 << bitIndex))
                {
                    Niu_OneBusSetDelay(OneBusSendCfg.Data1HDelayTime); //设置延迟
                    //设置延迟
                }else {
                    Niu_OneBusSetDelay(OneBusSendCfg.Data0HDelayTime); //设置延迟
                }

                if((OneBusSta.bitIndex++) == OneBusSta.bitSize)
                {
                    OneBusSta.bitPhase = 5;                    
                }else {
                    OneBusSta.bitPhase = 3;
                }


                break;
            case 5:
                MCU_GPIO_ClrBit(TN_ONE_TX_PORT, TN_ONE_TX_PIN);
                //设置延迟
                Niu_OneBusSetDelay(OneBusSendCfg.StopLDelayTime);
                OneBusSta.bitPhase = 6;
                break;
            case 6:
                MCU_GPIO_SetBit(TN_ONE_TX_PORT, TN_ONE_TX_PIN);
                OneBusSta.bitPhase = 0;
                Niu_OneBusTimerStop();
                break;
        }
    }
}